package com.hzbank.juc.pattern;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;

import static com.hzbank.juc.pattern.DownLoader.download;

/**
 * @description:
 * @author: libugu@cskaoyan.com
 * @date 2023/12/22 - 14:44
 */
@Slf4j
public class WaitIf01 {
    public static void main(String[] args) {
        GuardedObject guardedObject = new GuardedObject();
        new Thread(() -> {
            try {
                // 执行任务
                List<String> response = download();
                log.debug("download complete...");
                // 告诉守卫执行完成
                guardedObject.complete(response);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
        log.debug("waiting...");
        // 获取执行完成的结果
        Object response = guardedObject.get();
        log.debug("get response: [{}] lines", ((List<String>) response).size());
    }
}
